﻿using System;

namespace NazTek.Extension.Clr4
{
    /// <summary>
    /// Extends <see cref="Exception"/>
    /// </summary>
    public static class ExceptionExtension
    {
        /// <summary>
        /// Gets the inner most exception message
        /// </summary>
        /// <param name="ex">The exception to extract inner most exception from</param>
        /// <returns>Inner most exception</returns>
        public static Exception GetInnerMostException(this Exception ex)
        {
            if (ex.InnerException == null)
                return ex;
            return ex.GetInnerMostException();
        }
        /// <summary>
        /// Gets all exception messages from the top to the inner most, concatenated by space
        /// </summary>
        /// <param name="ex">The exception to extract messages from</param>
        /// <returns>Concatenated messages</returns>
        public static string GetConcatenatedExceptionMessage(this Exception ex)
        {
            if (ex.InnerException == null)
                return ex.Message;
            return String.Format("{0} ({1})", ex.Message, ex.InnerException.GetConcatenatedExceptionMessage());
        }
    }
}
